package com.cc.factory2;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.WebApplicationContext;

import com.cc.entity.User;
import com.cc.factory.BlackPig;
import com.cc.factory.RedPig;

public class Test01 {
    public static void main(String[] args) {
        //方式一：通过BeanFactory获取bean对象
        //1.获取beanFactory工厂对象
        Resource resource = new ClassPathResource("beans.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);
        System.out.println("=========");
        //2.beanFactory获取bean对象
        //2.1.getBean方式：id
        RedPig redPig = (RedPig) beanFactory.getBean("redPig");//根据bean的id获取bean对象
        System.out.println(redPig.hashCode());//hashcode:对象 地址的整数形式
        //2.2.getBean方式：class
//        RedPig redPig2 = (RedPig)  beanFactory.getBean(RedPig.class);
//        System.out.println(redPig2.hashCode());//hashcode：对象 地址的整数形式
        //2.3  getBean方式：class+id
        RedPig redPig3  = beanFactory.getBean("redPig", RedPig.class);
        System.out.println(redPig3.hashCode());
        System.out.println(redPig3);
        //2.4  getBean方式：id/class+构造方法参数
        BlackPig bp = (BlackPig) beanFactory.getBean("blackPig","大锤",22 );
        System.out.println("blackPig==>"+bp);
        
        RedPig redPig4 = (RedPig) beanFactory.getBean("redPig2");//根据bean的id获取bean对象
        System.out.println("redPig2====>"+redPig4.hashCode());//hashcode:对象 地址的整数形式
        
        
        //结论：redPig与redPig2是同一个对象（单例模式）
        
        //方式二：通过ApplictioonContext获取bean对象 继承BeanFactory接口
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        RedPig redPig5 = (RedPig) context.getBean("redPig");
        System.out.println(redPig5);
        
      //方式三：通过ApplictioonContext获取bean对象 继承BeanFactory接口
//        ApplicationContext ctx =
//                new FileSystemXmlApplicationContext("config/beans.xml");或
        ApplicationContext ctx =
                new FileSystemXmlApplicationContext("classpath:beans.xml");
        RedPig redPig6= (RedPig) ctx.getBean("redPig");
        System.out.println("redPig6==>"+redPig6);
        
        //方式四：spring-web-5.0.14.RELEASE.jar  
        //C# --->.net 点立得          java-->java EE
        WebApplicationContext context2;
        
        System.out.println("=====================静态工厂和实例工厂进行实例化========");
        User u01 = (User) context.getBean("u01");
        System.out.println("u01===>"+u01);
        User u02 = (User) context.getBean("u02");
        System.out.println("u02===>"+u02);
        System.out.println("=====================bean的作用域========");
        User u03 = (User) context.getBean("u03");
        System.out.println(u03.hashCode());
        User u04 = (User) context.getBean("u03");
        System.out.println(u04.hashCode());
        System.out.println("=====================bean的生命周期方法========");
        User u05 = (User) context.getBean("u04");
        System.out.println(u05);
    }
}
